<template>
  <div>
    <div @click="addNew" class="bottom">添加新闻</div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="newsCreatedAt" label="时间" width="170">
      </el-table-column>
      <el-table-column prop="name" label="标题" width="220"> </el-table-column>
      <el-table-column prop="source" label="来源" width="120">
      </el-table-column>
      <el-table-column prop="url" label="链接" width="320"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small"
            >修改</el-button
          >
          <el-button type="text" size="small" @click="deleteNew(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="4" 
        @current-change="currentChange"
      >
      </el-pagination>
    </div>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="70%" :before-close="close">
      <el-form label-position="right" label-width="80px" :model="form">
        <el-form-item label="标题">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="时间">
          <el-input v-model="form.newsCreatedAt"></el-input>
        </el-form-item>
        <!-- <el-form-item label="内容">
          <el-input
            type="textarea"
            :rows="20"
            v-model="form.content"
          ></el-input>
        </el-form-item> -->
        <el-form-item label="内容">
        <tinymce ref="tinymceContent" v-model="form.content" :height="400" v-show="flag"/>
        </el-form-item>
        <el-form-item label="来源">
          <el-input v-model="form.source"></el-input>
        </el-form-item>
        <el-form-item label="链接">
          <el-input v-model="form.url"></el-input>
        </el-form-item>
        <el-form-item label="是否置顶">
          <el-checkbox v-model="form.top"></el-checkbox>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Tinymce from '@/components/Tinymce'
export default {
  components: { Tinymce },
  data() {
    return {
      dialogVisible: false,
      form: {
        name: "",
        newsCreatedAt: "",
        content: "",
        source: "",
        url: "",
        top:""
      },
      tableData: [],
      newsList: [],
      total: 0,
      current: 1,
      ifEdit: false,
      flag:true
    };
  },
  mounted() {
    this.getNews();
  },
  methods: {
    close(){
      this.flag=false
      this.dialogVisible = false
    },
    handleClick(row) {
      // this.$refs.tinymceContent.setContent('')
      this.form = row;
      if(this.form.top==1){
        this.form.top=true
      }else{
        this.form.top=false
      }
      this.form.content=row.content
      this.dialogVisible = true;
      this.ifEdit = true;
      this.flag=true
    },
    currentChange(e) {
      this.current = e;
      this.getNews()
    },
    addNew() {
      this.dialogVisible = true;
      this.ifEdit = false;
    },
    async getNews() {
      const res = await this.$indexApi.getNews({
        current: this.current,
        size: 4,
      });
      if (res.code == 0) {
        this.tableData = res.data.records;
        this.total = res.data.total;
        this.form={}
      }
    },
    async submit() {
      var top=0
      if(this.form.top){
        top=1
      }else{
        top=0
      }
      if (this.ifEdit) {
        const res = await this.$indexApi.addNews({
          id: this.form.id,
          name: this.form.name,
          source: this.form.source,
          url: this.form.url,
          newsCreatedAt: this.form.newsCreatedAt,
          content: this.form.content,
          top:top
        });
        if (res.code == 0) {
          this.getNews();
          this.dialogVisible = false;
          this.form={
            name: "",
            newsCreatedAt: "",
            content: "",
            source: "",
            url: "",
          }
        }
      } else {
        const res = await this.$indexApi.addNews({
          name: this.form.name,
          source: this.form.source,
          url: this.form.url,
          newsCreatedAt: this.form.newsCreatedAt,
          content: this.form.content,
          top:top
        });
        if (res.code == 0) {
          this.getNews();
          this.dialogVisible = false;
          this.form={
            name: "",
            newsCreatedAt: "",
            content: "",
            source: "",
            url: "",
            top:"",
          }
        }
      }
    },
    async deleteNew(row) {
      const res = await this.$indexApi.deleteNew({
        id: row.id,
      });
      if (res.code == 0) {
        this.getNews();
      }
    },
  },
};
</script>

<style lang = "scss" scoped>
.bottom {
  height: 30px;
  margin: 40px;
  display: flex;
  justify-content: flex-end;
  cursor: pointer;
}
.pagination {
  margin: 10px;
  display: flex;
  justify-content: flex-end;
}
</style>
  